<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>${book.title} - 图书详情</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .book-detail {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .book-cover {
            width: 100%;
            height: 400px;
            background-color: #e9ecef;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #6c757d;
            border-radius: 10px;
        }
        .book-info {
            padding: 20px;
        }
        .status-available {
            color: #28a745;
            font-weight: bold;
        }
        .status-unavailable {
            color: #dc3545;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="fas fa-book"></i> 图书借阅系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/book/list">图书列表</a>
                    </li>
                </ul>
                <ul class="navbar-nav">
                    <c:choose>
                        <c:when test="${not empty sessionScope.reader}">
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/dashboard">个人中心</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/logout">退出登录</a>
                            </li>
                        </c:when>
                        <c:otherwise>
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/login">读者登录</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="/reader/register">读者注册</a>
                            </li>
                        </c:otherwise>
                    </c:choose>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="book-detail">
            <div class="row">
                <div class="col-md-4">
                    <div class="book-cover">
                        <i class="fas fa-book fa-5x"></i>
                    </div>
                </div>
                <div class="col-md-8">
                    <div class="book-info">
                        <h2>${book.title}</h2>
                        <p class="text-muted">作者: ${book.author}</p>
                        
                        <table class="table">
                            <tr>
                                <td><strong>ISBN:</strong></td>
                                <td>${book.isbn}</td>
                            </tr>
                            <tr>
                                <td><strong>出版社:</strong></td>
                                <td>${book.publisher}</td>
                            </tr>
                            <tr>
                                <td><strong>出版日期:</strong></td>
                                <td>${book.publishDate}</td>
                            </tr>
                            <tr>
                                <td><strong>分类:</strong></td>
                                <td>${book.categoryName}</td>
                            </tr>
                            <tr>
                                <td><strong>总数量:</strong></td>
                                <td>${book.totalQuantity}</td>
                            </tr>
                            <tr>
                                <td><strong>可借数量:</strong></td>
                                <td>
                                    <span class="${book.availableQuantity > 0 ? 'status-available' : 'status-unavailable'}">
                                        ${book.availableQuantity}
                                    </span>
                                </td>
                            </tr>
                        </table>
                        
                        <div class="mt-4">
                            <c:if test="${not empty sessionScope.reader}">
                                <c:choose>
                                    <c:when test="${book.availableQuantity > 0}">
                                        <button class="btn btn-success btn-lg" onclick="borrowBook(${book.id})">
                                            <i class="fas fa-hand-holding"></i> 立即借阅
                                        </button>
                                    </c:when>
                                    <c:otherwise>
                                        <button class="btn btn-secondary btn-lg" disabled>
                                            <i class="fas fa-times"></i> 暂无可借
                                        </button>
                                    </c:otherwise>
                                </c:choose>
                            </c:if>
                            
                            <c:if test="${empty sessionScope.reader}">
                                <a href="/reader/login" class="btn btn-primary btn-lg">
                                    <i class="fas fa-sign-in-alt"></i> 登录后借阅
                                </a>
                            </c:if>
                            
                            <a href="/book/list" class="btn btn-outline-secondary btn-lg ms-2">
                                <i class="fas fa-arrow-left"></i> 返回列表
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="row mt-4">
                <div class="col-12">
                    <h4>图书简介</h4>
                    <p>${book.description}</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="bg-dark text-light mt-5 py-4">
        <div class="container text-center">
            <p>&copy; 2024 图书借阅系统. 保留所有权利.</p>
        </div>
    </footer>

    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script>
        function borrowBook(bookId) {
            if (confirm('确定要借阅这本书吗？')) {
                $.post('/borrow/borrow', {bookId: bookId}, function(response) {
                    if (response === '借阅成功') {
                        alert('借阅成功！');
                        location.reload();
                    } else {
                        alert(response);
                    }
                });
            }
        }
    </script>
</body>
</html>